package main.java.jdbc3;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.google.common.annotations.VisibleForTesting;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

/**
 * @Auther:Liu
 * @Testname:Druid
 * @Date:2025/3/12 14:38
 */
public class Druid {
    public static void main(String[] args) throws Exception {
        Druid druid = new Druid();
//        druid.findmax();
//        druid.insert();
//        druid.delete();
//        druid.update();
    }

    //链接
    public Connection Connection() throws Exception {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/jdbc3/Druidjdbc.properties");
        properties.load(fileInputStream);
        //德鲁伊连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        Connection connection = dataSource.getConnection();
//        System.out.println(connection);
        return connection;
    }

    //查找
    public void findmax() throws Exception {
        Connection connection = Connection();
        String sql = "select * from dept";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        ArrayList<Dept> depts = new ArrayList<>();
        while (resultSet.next()) {
            Dept dept = new Dept();
            dept.setDeptno(resultSet.getInt("DEPTNO"));
            dept.setDname(resultSet.getString("DNAME"));
            dept.setLoc(resultSet.getString("LOC"));
            depts.add(dept);
        }
        System.out.println("" + depts);
    }

    //插入
    public void insert() throws Exception {
        Connection connection = Connection();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要插入的id:");
        int id = scanner.nextInt();
        System.out.println("请输入要插入的dname:");
        String name = scanner.next();
        System.out.println("请输入要插入的loc:");
        String loc = scanner.next();
        String sql = "insert dept values(?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        statement.setString(2, name);
        statement.setString(3, loc);
        int i = statement.executeUpdate();
        if (i != 0) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }
    }

    //删除
    public void delete() throws Exception {
        Connection connection = Connection();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的id:");
        int id = scanner.nextInt();
        String sql = "delete from dept where DEPTNO=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        int i = statement.executeUpdate();
        if (i != 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    //更新
    public void update() throws Exception {
        Connection connection = Connection();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的dname:");
        String name = scanner.next();
        System.out.println("请输入要修改的loc:");
        String loc = scanner.next();
        System.out.println("请输入要修改的id:");
        int id = scanner.nextInt();
        String sql = "update dept set DNAME=?,LOC=? WHERE DEPTNO=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name);
        statement.setString(2, loc);
        statement.setInt(3, id);
        int i = statement.executeUpdate();
        if (i != 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
    }


}
